<?php
/**
 * 订单核销
 * User: lxq
 * Date: 2018/5/9
 * Time: 13:44
 */
class order_doconsumeControl extends  SystemControl {


    public  function  __construct()
    {
        parent::__construct();
        Language::read('order');
    }

    public function indexOp()
    {
        //查询条件
        $condition = array();
        $condition['order_status'] = 20;
        $merchant_id = intval($this->admin_info['merchant_id']);
        if($merchant_id>0){
            $condition['order.merchant_id']  = intval($this->admin_info['merchant_id']);
        }
        if (isset($_GET['search_sn']) && $_GET['search_sn']) {
            $condition['ticket_code'] = $_GET['search_sn'];
        }else{
            $condition['ticket_code'] = 0;
        }
        /* @var orderModel $model */
        $model = Model('order');
        $table = 'order,member,member_merchant';
        $on = 'member.member_id=order.member_id,member_merchant.merchant_id=order.merchant_id';
        $field = array(
            'order.order_id',
            'order.order_sn',
            'order.ticket_title',
            'order.need_pay',
            'order.add_time',
            'order.order_status',
            'order.merchant_id',
            'order.order_type',
            'order.spec_name',
            'order.ticket_code',
            'order.if_examine',
            'member.nickname',
            'member.mobile',
            'member_merchant.mc_shop_name'
        );
        //订单列表
        $orderList = $model
            ->table($table)
            ->field(implode(',',$field))
            ->join('left,left')
            ->on($on)
            ->where($condition)
            ->order('add_time desc')
            ->select();
        /* @var order_ticketModel $order_ticket_model */
        $order_ticket_model = Model('order_ticket');
        foreach ($orderList as $k => $order) {
            switch($order['order_type']){
                case 1:
                    $orderList[$k]['sku'] = Model('order_hotel')->getOrderHotelList(array('order_id'=>$order['order_id']));
                    break;
                case 2:
                    $orderList[$k]['sku'] = $order_ticket_model->getOrderTicketList(array('order_id'=>$order['order_id']));
                    break;
                case 3:
                    $orderList[$k]['sku'] = Model('order_goods')->getOrderGoodsList(array('order_id'=>$order['order_id']));
                    break;
                case 4:
                    $orderList[$k]['sku'] = Model('order_goods')->getOrderGoodsList(array('order_id'=>$order['order_id']));
                    break;
            }
        }
        if($orderList['order_type']==1 && $orderList[0]['if_examine']!=1){
            unset($orderList);
        }
        Tpl::output('orders', $orderList);
        Tpl::output('search',$_GET);
        Tpl::output('status', $model->status);
        Tpl::output('order_type',$model->type);
        Tpl::showpage('order_doconsume.index');
    }

    /**
     * 订单核销
     */
    public function consumeOp()
    {
        /* @var orderModel $order_model */
        $order_model = Model('order');
        $code = trim($_POST['ticket_code']);
        if( $code ){
            $condition = array();
            $condition['order_status'] = 20;
            $condition['ticket_code'] = $code;
            if($this->admin_info['merchant_id']>0){
                $condition['merchant_id'] = $this->admin_info['merchant_id'];
            }
            $order = $order_model->getOrderInfo($condition);
            if($order['order_type']==1 && $order['if_examine']!=1){
                exit(json_encode(array('ecode'=>0,'msgs'=>'该酒店订单还未审核,请先确认您的审核操作')));
            }

            if($order){
                Model::beginTransaction();
                try{
                    $res = $order_model->updateOrder(array('order_id'=>$order['order_id']), array('order_status'=>21,'used_time'=>time()));
                    //增加商家的钱包金额
                    $merchant_model = Model('member_merchant');
                    $res_2 =$merchant_model->addMerchantMoney($order['merchant_id'],'all_money',$order['need_pay']);
                    if($res && $res_2){
                        //添加商家金额记录
                        $pd_log_model = Model('pd_log');
                        $pd_log_model->addPd_log(array(
                            'merchant_id'=>$order['merchant_id'],
                            'lg_member_name'=>$_SESSION['member']['mobile'],
                            'lg_type'=>1,//收入
                            'lg_av_amount'=>$order['need_pay'],
                            'lg_add_time'=>time(),
                            'lg_desc'=>'订单'.'['.$order['order_sn'] .']'.'核销成功,您收入'.$order['need_pay'].'元',
                            'order_sn'=>$order['order_sn'],
                            'order_name'=>$order['ticket_title'],
                        ));
                        Model::commit();
                        $this->log('核销订单'.'[订单号:'.$order['order_sn'].']',1);
                        exit(json_encode(array('ecode'=>1,'msgs'=>'核销成功')));
                    }else{
                        Model::rollback();
                        exit(json_encode(array('ecode'=>0,'msgs'=>'系统原因,核销失败')));
                    }
                }catch (\Exception $e){
                    Model::rollback();
                    exit(json_encode(array('ecode'=>0,'msgs'=>$e->getMessage())));
                }
            }else{
                exit(json_encode(array('ecode'=>0,'msgs'=>'订单不存在或已核销')));
            }
        }else{
            exit(json_encode(array('ecode'=>0,'msgs'=>'非法操作')));
        }
    }
}